#include <QPalette>
#include <QColor>
#include <QPainter>
#include <QPen>

#include "buttonPlay.h"

buttonPlay::buttonPlay(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(50, 50);
    this->setCursor(Qt::PointingHandCursor);
    isMouseIn = 0;
    playState = Pausing;


}


void buttonPlay::paintEvent(QPaintEvent *event)
{
    (void) event;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.fillRect(this->rect(), QColor(0, 0, 0, 0));

    QPen pen;
    pen.setWidth(1);
    pen.setCapStyle(Qt::RoundCap);
    if(isMouseIn == 1)
    {
        pen.setColor(QColor(0xFF, 0xFF, 0xFF, 0xFF));
    }
    else
    {
        pen.setColor(QColor(0xEE, 0xEE, 0xEE, 0xEE));
    }
    painter.setPen(pen);

    painter.drawEllipse(5, 5, 40, 40);


    pen.setColor(QColor(0xEE, 0xEE, 0xEE, 0xEE));
    pen.setWidth(3);
    painter.setPen(pen);

    QBrush brush;
    brush.setColor(QColor(0xEE, 0xEE, 0xEE, 0xEE));
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);


    if(playState == Pausing)
    {
        int s = 15;
        QPointF points[3] = {QPointF(25-0.5*s, 25-0.866*s),
                             QPointF(40, 25),
                             QPointF(25-0.5*s, 25+0.866*s)};
        painter.drawPolygon(points, 3);
    }
    else
    {
        pen.setWidth(6);
        painter.setPen(pen);
        painter.drawLine(QPointF(18, 15), QPointF(18, 35));
        painter.drawLine(QPointF(32, 15), QPointF(32, 35));
    }
}



void buttonPlay::enterEvent(QEvent *event)
{
    (void) event;

    this->isMouseIn = 1;
    this->update();
}

void buttonPlay::leaveEvent(QEvent *event)
{
    (void) event;

    this->isMouseIn = 0;
    this->update();
}


void buttonPlay::mousePressEvent(QMouseEvent *event)
{
    (void) event;

    if(playState == Playing)
    {
        playState = Pausing;
        emit stateChanged(PAUSE);
    }
    else
    {
        playState = Playing;
        emit stateChanged(PLAY);
    }

    update();
}


void buttonPlay::setState(int state)
{
    if(state == PAUSE)
    {
        playState = Pausing;
    }
    else if(state == PLAY)
    {
        playState = Playing;
    }
    update();
}
